package com.xilihui.xlh.business.activitys.store;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.xilihui.xlh.R;
import com.xilihui.xlh.business.activitys.SearchShopActivity;
import com.xilihui.xlh.business.entities.SearchGoodsEntity;
import com.xilihui.xlh.business.requests.StoreRequest;
import com.xilihui.xlh.business.widget.StorePopWindow;
import com.xilihui.xlh.component.recyclerview.BaseAdapter;
import com.xilihui.xlh.component.recyclerview.BaseViewHolder;
import com.xilihui.xlh.constant.UrlConst;
import com.xilihui.xlh.core.app.BaseCompatActivity;
import com.xilihui.xlh.core.http.DoTransform;
import com.xilihui.xlh.core.http.HttpSubscriber;
import com.xilihui.xlh.core.util.YEventBuses;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/9/4.
 */

public class StoreSearchActivity extends BaseCompatActivity implements OnRefreshListener, OnLoadMoreListener {
    
    @BindView(R.id.tv_toolbar_right)
    TextView tv_toolbar_right;
    
    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerView;
    
    StorePopWindow popWindow;
    
    ArrayList<SearchGoodsEntity.GoodsBean> data = new ArrayList<>();
    BaseAdapter<SearchGoodsEntity.GoodsBean> beanBaseAdapter;
    
    String keyword = "";
    String orderby = "";
    int page = 1;
    
    @OnClick(R.id.iv_toolbar_left)
    public void back(){
        finish();
    }
    
    @OnClick(R.id.tv_toolbar_title)
    public void toSearch(){
        Intent intent = new Intent(this,StoreHisSearchActivity.class);
        startActivity(intent);
    }
    
    @OnClick(R.id.tv_toolbar_right)
    public void sift(){
        popWindow = new StorePopWindow(this);
        popWindow.show(tv_toolbar_right);
    }
    
    @Override
    public int getLayout() {
        return R.layout.activity_store_search;
    }

    @Override
    public String initTitle() {
        return null;
    }

    @Override
    public void initUI(@Nullable final Bundle savedInstanceState) {
        
        smartRefreshLayout.setOnRefreshListener(this);
        smartRefreshLayout.setOnLoadMoreListener(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        beanBaseAdapter = new BaseAdapter<SearchGoodsEntity.GoodsBean>(this,data) {
            @Override
            protected int setLayoutId() {
                return R.layout.recycyler_item_shop_good_new;
            }

            @Override
            protected void getView(BaseViewHolder holder, SearchGoodsEntity.GoodsBean entity, int position) {
                SimpleDraweeView simpleDraweeView = (SimpleDraweeView)holder.getView(R.id.sdv);
                simpleDraweeView.setImageURI(UrlConst.baseUrl()+entity.getOriginal_img());
                
                holder.setText(R.id.tv_title,entity.getGoods_name());
                holder.setText(R.id.tv_num,"已售"+entity.getSales_sum()+"件");
                holder.setText(R.id.tv_price,"￥"+entity.getShop_price());
                
            }
        };
        
        recyclerView.setAdapter(beanBaseAdapter);
        beanBaseAdapter.setAnimationsLocked(true);
        getData(true);
        
                
    }

    @Override
    public void initData() {
        
    }
    
    public void getData(boolean isLoading){
        StoreRequest.searchGoods(keyword,orderby,page)
                .compose(DoTransform.<SearchGoodsEntity>applyScheduler(this,isLoading))
                .compose(this.<SearchGoodsEntity>bindToLifecycle())
                .subscribe(new HttpSubscriber<SearchGoodsEntity>(this) {
                    @Override
                    protected void onSuccess(SearchGoodsEntity entity) {
                        
                        if(page==1){
                            data.clear();
                        }
                        
                        data.addAll(entity.getGoods());
                        beanBaseAdapter.setList(data);
                        
                        if(page>=entity.getPageCount()){
                            smartRefreshLayout.finishLoadMoreWithNoMoreData();
                        }else {
                            smartRefreshLayout.setNoMoreData(false);
                        }
                        
                        smartRefreshLayout.finishRefresh();
                        smartRefreshLayout.finishLoadMore();
                        
                    }

                    @Override
                    protected void onFail(String msg) {
                        smartRefreshLayout.finishRefresh();
                        smartRefreshLayout.finishLoadMore();
                    }
                });
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        page = 1;
        getData(false);
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        page = page + 1;
        getData(false);
    }

    @Override
    public void onEventMainThread(YEventBuses.Event event) {
        super.onEventMainThread(event);
        
        if(event.is(StorePopWindow.STORE_SIFT)){
            String type = (String) event.getParams(0);
            page = 1;
            if(type.equals("1")){
                orderby = "sales_sum";//商品销量
            }else if(type.equals("2")){
                orderby = "goods_id";//发布时间
            }else if(type.equals("3")){
                orderby = "price_down";//价格降序
            }else if(type.equals("4")){
                orderby = "price_up";//价格升序
            }
            getData(true);  
        }else if(event.is(StoreHisSearchActivity.search)){
            keyword = (String) event.getParams(0);
            getData(true);
        }
    }
    
}
